package top.myliangsl.common.lib.domain.resp;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用的响应实体
 *
 * @author xiaoliang
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ApiResp<T> implements Serializable {
    public static final int SUCCESS_CODE = 0;
    public static final int FAIL_CODE = -1;


    private Integer code;

    private String msg;

    private T data;


    public static <T> ApiResp<T> success(Integer code, String msg, T data) {
        return new ApiResp<>(code, msg, data);
    }

    public static ApiResp success(Integer code, String msg) {
        return ApiResp.builder().code(code).msg(msg).build();
    }

    public static ApiResp success(String msg) {
        return ApiResp.builder().code(SUCCESS_CODE).msg(msg).build();
    }

    public static <T> ApiResp<T> fail(Integer code, String msg, T data) {
        return new ApiResp<>(code, msg, data);
    }

    public static ApiResp fail(Integer code, String msg) {
        return ApiResp.builder().code(code).msg(msg).build();
    }

    public static ApiResp fail(String msg) {
        return ApiResp.builder().code(FAIL_CODE).msg(msg).build();
    }
}
